package com.drama.api.sys.authority.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.ApiIndex;
import com.drama.api.base.IDEntity;
import com.drama.api.base.R;
import com.drama.api.sys.authority.domain.vo.SysAuthorityTreeSearchVO;
import com.drama.api.sys.authority.domain.vo.SysAuthorityTreeVO;
import com.drama.api.sys.authority.service.ISysAuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


@Api( tags = ApiIndex.SysAuthorityController)
@RequestMapping(value = "/api/sys/authority")
@RestController
public class SysAuthorityController {

    @Autowired
    private ISysAuthorityService service;

    @ApiOperation(value = "查询所有系统权限(树形)")
    @GetMapping(value = "/tree")
    public R<List<SysAuthorityTreeVO>> tree(
            @ApiParam(value = "查询参数") @ModelAttribute SysAuthorityTreeSearchVO searchVO) {
        List<SysAuthorityTreeVO> list = service.getTreeList(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "查询当前登录用户拥有权限菜单")
    @GetMapping(value = "/user_tree")
    public R<List<SysAuthorityTreeVO>> user_tree() {
        SysAuthorityTreeSearchVO sysAuthorityTreeSearchVO=new SysAuthorityTreeSearchVO();
        sysAuthorityTreeSearchVO.setUserId(StpUtil.getLoginId().toString());
        List<SysAuthorityTreeVO> list = service.getTreeList(sysAuthorityTreeSearchVO);
        return new R(list);
    }

    @ApiOperation(value = "查询角色拥有的权限菜单IDs")
    @GetMapping(value = "/role_ids")
    public R<List<String>> role_ids(@ApiParam(value = "角色ID") @Valid @ModelAttribute IDEntity roleId) {
        SysAuthorityTreeSearchVO sysAuthorityTreeSearchVO=new SysAuthorityTreeSearchVO();
        sysAuthorityTreeSearchVO.setRoleId(roleId.getId());
        List<String> list = service.getAuthorityIds(sysAuthorityTreeSearchVO);
        return new R(list);
    }

}
